package com.mx.project.controller;

import com.mx.project.common.resp.ApiResult;
import com.mx.project.common.util.JWTService;
import com.mx.project.pojo.Cart;
import com.mx.project.vo.CartInfo;
import com.mx.project.services.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Api(tags = "用于操作购物车接口")
@RestController
@RequestMapping("cart")
public class CartController {

    @Autowired
    private JWTService jwtService;

    @Autowired
    private CartService cartService;

    @ApiOperation("加入购物车接口 , cart中要包含:pid(产品ID)  count(数量) , 此请求要携带token")
    @PostMapping()
    public ApiResult storeCart(@RequestBody Cart cart, HttpServletRequest request){
        //先判断后端是否已经有这个商品了  如果有count＋1 即可
        ApiResult result = new ApiResult();
        Integer  uid = jwtService.getUserId(request.getHeader("token"));
        Cart ct = cartService.getCartBySidAndUid(cart.getPid(),uid);
        if(ct != null){
            cartService.cartCountAdd(uid,ct.getSid(),cart.getCount());
        }else{
            //根据时间戳的后八位 进行生成编号
            String number = new Date().getTime() + "";
            cart.setNumber(Integer.valueOf(number.substring(number.length() - 8)));
            cart.setCreateTime(new Date());
            cart.setUid(uid);
            cartService.addCart(cart);
        }
        return result;
    }

    @ApiOperation("获取用户购物车列表")
    @GetMapping()
    public ApiResult<List<CartInfo>> getCart(HttpServletRequest request){
        ApiResult<List<CartInfo>> result = new ApiResult();
        Integer uid = jwtService.getUserId(request.getHeader("token"));
        List<CartInfo> list =  cartService.getCartByUid(uid);
        result.setData(list);
        return result;
    }

    @ApiOperation("删除购物车")
    @DeleteMapping("/{pid}")
    public ApiResult delCart(@PathVariable Integer pid,HttpServletRequest request){
        String token = request.getHeader("token");
        Integer uid = jwtService.getUserId(token);
        ApiResult result = new ApiResult();
        int count =  cartService.removeCartByPidAndUid(pid,uid);
        if(count < 1){
            //todo
        }
        return result;
    }

    @ApiOperation("清除当前用户的购物车")
    @DeleteMapping("")
    public ApiResult delAllCart(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer uid = jwtService.getUserId(token);
        ApiResult result = new ApiResult();
        int count =  cartService.removeCartByUid(uid);
        if(count < 1){
            //todo
        }
        return result;
    }

    @ApiOperation("提交购物车到订单页")
    @PostMapping("/submit")
    public ApiResult submit(@RequestBody List<CartInfo> lists, HttpServletRequest request){
        ApiResult result = new ApiResult();
        String token = request.getHeader("token");
        Integer uid = jwtService.getUserId(token);
        int count = cartService.updateMulCartCount(uid,lists);
        return result;
    }

    @ApiOperation("删除")
    @RequestMapping("/test")
    public ApiResult del(HttpServletRequest request){
        String token = request.getHeader("token");
        Integer uid = jwtService.getUserId(token);
        ApiResult result = new ApiResult();
        int count =  cartService.removeCartByUid(uid);
        return result;
    }





}
